package co.china.lottery.account.action;




import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import co.china.lottery.account.action.base.BaseAction;
import co.china.lottery.account.service.CreateAccountService;
import co.china.lottery.account.service.bean.AccountRequest;
import co.china.lottery.account.service.bean.AccountResponse;
import co.china.lottery.account.service.bean.AccountTransReq;
import co.china.lottery.account.service.bean.AgentAccountQueryResp;
import co.china.lottery.account.service.bean.ResultCode;
import co.china.lottery.account.service.bean.TransferResponse;
import co.china.lottery.account.service.code.PayDecrementCode;
import co.china.lottery.account.service.code.PayIncrementCode;
import co.china.lottery.account.service.code.PrizeIncrementCode;
import co.china.lottery.account.service.code.base.Account;
import co.china.lottery.account.service.impl.PayAccountService;
import co.china.lottery.account.service.impl.PrizeAccountService;


@Controller("accountAction")
@Scope("prototype")
public class AccountAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private PayAccountService payAccount;
	
	@Autowired
	private PrizeAccountService prizeAccount;
	
	@Autowired
	private CreateAccountService[] createAccountService;
	
	
	/**
	 * 1.1   创建账户
	 */
	public void createAccount(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		
		if(createAccountService == null || createAccountService.length == 0) {
            log.error("cannot find any CreateAccountService");
            this.buildResponse(ResultCode.createSuccess(ResultCode.class));
            return ;
        }

        // 逐个创建各账户
        for(int i = 0; i < createAccountService.length; i++){
        	Account entity = createAccountService[i].getOrCreateAccount(agentId, false);
        	log.info("createAccount accountId:{}",entity.getId());
        }
		this.buildResponse(ResultCode.createSuccess(ResultCode.class));
	}
	
	/**
	 * 1.2   商户资金账户余额查询(包括奖金与彩金)
	 */
	public void agentAccountQuery(){
		
		//包括彩金余额,奖金账户余额
		String agentId = getRequest().getParameter("agentId");//商户号
		
		BigDecimal payBalance =  payAccount.newAgentAccountQuery(agentId);
		BigDecimal prizeBalance =  prizeAccount.newAgentAccountQuery(agentId);
		AgentAccountQueryResp resp = ResultCode.createSuccess(AgentAccountQueryResp.class);
		resp.setPayBalance(payBalance);
		resp.setPrizeBalance(prizeBalance);
		this.buildResponse(resp);
	}
	
	/**
	 * 1.3  彩金账户充值
	 */
	public void increment(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//充值金额(元 double)
		// 管理员
		String createUser = getRequest().getParameter("createUser");
		// 备注
		String remark = getRequest().getParameter("remark");   
		
		String accountCode = PayIncrementCode.CODE_1000.getCode();
		
		AccountRequest req = new AccountRequest(agentId, money, accountCode, createUser, remark);

		AccountResponse resp =  payAccount.increment(req);
		
		this.buildResponse(resp);
	}
	

	 
	/**
	 * 1.4  彩金账户扣费(购彩)
	 */
	public void betDecrement(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//充值金额(元 double)
		String accountCode = PayDecrementCode.CODE_2101.getCode();
		String serviceId = getRequest().getParameter("serviceId");
		AccountRequest req = new AccountRequest(agentId, money, accountCode, serviceId);
        AccountResponse resp =  payAccount.decrement(req);
		this.buildResponse(resp);

		
		
	}
	
	/**
	 * 1.5  彩金账户冲正(购彩失败退费)
	 */
	public void betRefund(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//充值金额(元 double)
		String accountCode = PayIncrementCode.CODE_1105.getCode();
		String serviceId = getRequest().getParameter("serviceId");
		// 管理员
		String createUser = getRequest().getParameter("createUser");
		// 备注
		String remark = getRequest().getParameter("remark");   
		AccountRequest req = new AccountRequest(agentId, money, accountCode, serviceId,createUser,remark);

		AccountResponse resp =  payAccount.increment(req);
		
		this.buildResponse(resp);
	}
	
	/**
	 * 1.6   派奖(奖金账户充值)--单个订单---竞彩
	 */
	public void prizeIncrement(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//总金额
		String batchId = getRequest().getParameter("batchId");//批次号
		
		String accountCode = PrizeIncrementCode.CODE_1301.getCode();
		String serviceId = getRequest().getParameter("serviceId");
		
		AccountRequest req = new AccountRequest(agentId, money, accountCode, serviceId, batchId,null,null);

		AccountResponse resp =  prizeAccount.increment(req);
		
		this.buildResponse(resp);
	}
	
	
	/**
	 * 1.7  派奖(奖金账户充值)--批量充值(普通彩种派奖)
	 */
	public void prizeBatchIncrement(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//总金额
		String batchId = getRequest().getParameter("batchId");//批次号
		
		String accountCode = PrizeIncrementCode.CODE_1301.getCode();
		
		String prizeDetail = getRequest().getParameter("prizeDetail");//订单中奖明细
		
		AccountRequest req = new AccountRequest(agentId, money, accountCode, null, batchId, null, null, prizeDetail);

		AccountResponse resp =  prizeAccount.prizeBatchIncrement(req);
		
		this.buildResponse(resp);
	}
	
	/**
	 * 1.8   派奖(奖金账户充值)--批次提交
	 */
	public void prizeBatchSubmit(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String batchId = getRequest().getParameter("batchId");//批次号
		String money = getRequest().getParameter("money");
		
		AccountRequest req = new AccountRequest(agentId, money, null, null, batchId,null,null);

		AccountResponse resp =  prizeAccount.prizeBatchSubmit(req);
		
		this.buildResponse(resp);
	}
	
	/**
	 * 1.9  奖金转彩金(将奖金转入彩金账户)
	 */
	public void prizeTransAccount(){
		
		String agentId = getRequest().getParameter("agentId");//商户号
		String money = getRequest().getParameter("money");//转账金额
		// 管理员
		String createUser = getRequest().getParameter("createUser");
		// 备注
		String remark = getRequest().getParameter("remark"); 

		AccountTransReq req = new AccountTransReq(agentId,money,createUser, remark);

		TransferResponse resp =  prizeAccount.transfer(req);
		
		this.buildResponse(resp);
	}
	
	public void newPrizeTransAccount(){
		
		String agentId = getRequest().getParameter("agentId");
		String money = getRequest().getParameter("money");
		// ����Ա
		String createUser = getRequest().getParameter("createUser");
		// ��ע
		String remark = getRequest().getParameter("remark"); 

		AccountTransReq req = new AccountTransReq(agentId,money,createUser, remark);

		TransferResponse resp =  prizeAccount.newTransfer(req);
		
		this.buildResponse(resp);
	}
	
	/**
	 * 2.0  彩金账户/奖金帐户扣费
	 */
	public void decAccountMoney(){
		
		// 管理员(操作人)
		String createUser = getRequest().getParameter("createUser");
		String agentId = getRequest().getParameter("agentId");//商户号
		String moneyType = getRequest().getParameter("moneyType");
		String money = getRequest().getParameter("money");
		String remark = getRequest().getParameter("remark");
		String accountCode = PayDecrementCode.CODE_2000.getCode();
		//String serviceId = getRequest().getParameter("serviceId");
		AccountRequest req = new AccountRequest(agentId, money, accountCode, createUser,remark);
		AccountResponse resp = null;
		if("1".equals(moneyType)){
			resp =  payAccount.decrement(req);
		}else if("2".equals(moneyType)){
			resp =  prizeAccount.decrement(req);
		}
		
		this.buildResponse(resp);
	}
	
}